﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BskLeague.Models;

namespace BskLeague.Controllers
{ 
    public class ScheduleController : Controller
    {
        private BskLeagueDB db = new BskLeagueDB();

        //
        // GET: /Schedule/

        public ViewResult Index()
        {
            var sortedMatches =
                (from m in db.Matchs
             //    where m.Season.Status.Status=="Active"
                orderby m.Date 
                select m).OrderByDescending(p=>p.Date).ToList();

            List<SelectListItem> itemsForDropDown = sortedMatches.Select(p => p.Round).Distinct().OrderBy(p => p).Select(c => new SelectListItem
            {
                Value = c.ToString(),
                Text = c.ToString()
            }).ToList();
            itemsForDropDown.Insert(0,new SelectListItem { Value = "-1", Text = "Wszystkie", Selected = true });
            

            ViewBag.itemsForDropDown = itemsForDropDown.ToList();

            var allSeasons = db.Seasons.OrderBy(p => p.SeasonId).ToList();
            List<SelectListItem> seasonsForDropDown = allSeasons.Select(c => new SelectListItem
            {
                Value = c.SeasonId.ToString(),
                Text = c.SeasonName.ToString()
            }).ToList();

            seasonsForDropDown.Insert(0,new SelectListItem { Value = "-1", Text = "Wszystkie", Selected = true });
            ViewBag.seasonsForDropDown = seasonsForDropDown.ToList();

             List<SelectListItem> gamesTypes = new List<SelectListItem>();
             gamesTypes.Add(new SelectListItem { Value="All", Text="Wszystkie", Selected=true });
             gamesTypes.Add(new SelectListItem { Value = "Played", Text = "Rozegrane" });
             gamesTypes.Add(new SelectListItem { Value = "ToPlay", Text = "Do rozegrania" });
             ViewBag.gamesTypes = gamesTypes;

            /////// najblizsze mecze !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!




             var stadiums = db.Stadiums.ToList();
             List<SelectListItem> stadiumsForDropDown = stadiums.Select(c => new SelectListItem
             {
                 Value = c.stadiumId.ToString(),
                 Text = c.stadiumName.ToString()
             }).ToList();

             stadiumsForDropDown.Insert(0,new SelectListItem { Value = "-1", Text = "Wszystkie", Selected = true });
             ViewBag.stadiumsForDropDown = stadiumsForDropDown.ToList();

            return View(sortedMatches);
        }

        [HttpPost]
        public ActionResult Filter(int season, int round, int stadium, String gamesTypes)
        {
            var matchesSel = db.Matchs.ToList();
            if (-1 != season)
            {
                matchesSel = matchesSel.Where(s => s.Season.SeasonId == season).ToList();
            }


            if (-1 != round)
            {
                matchesSel = matchesSel.Where(s => s.Round == round).ToList();
            }

            if (-1 != stadium)
            {
                matchesSel = matchesSel.Where(s => s.Place.stadiumId == stadium).ToList();
            }

            if(gamesTypes.Equals("Played"))
            {
                matchesSel = matchesSel.Where(s => s.Confirmed1==true && s.Confirmed2==true).ToList();
            }
            if (gamesTypes.Equals("ToPlay"))
            {
                matchesSel = matchesSel.Where(s => s.Confirmed1 == false || s.Confirmed2 == false).ToList();
            }
            matchesSel = matchesSel.OrderBy(a => a.Date).ToList();
            return PartialView("ListMatchesPartial", matchesSel);
        }


        class PlayerStats
        {
            
            public Player player;
            public int time;
            public int points;

        }

        //
        // GET: /Schedule/Details/5

        public ViewResult Details(int id)
        {
            Match match = db.Matchs.Find(id);
          /*  var points = db.Points.Where(p => p.Match.MatchId == match.MatchId);
            var players = db.Players.Where(p => p.Team.TeamId == match.Team1.TeamId);
            List<PlayerStats> ps = new List<PlayerStats>();

            foreach (Player p in players)
            {
                PlayerStats s = new PlayerStats();
                s.player=p;
                s.points=points.Where(a=> a.Player.PlayerId==p.PlayerId).Select(a=>a.ScoredPoints).FirstOrDefault();
                


            }

            ViewBag.Players1 = db.Players.Where(p => p.Team.TeamId == match.Team1.TeamId);
           */
            return View(match);
        }

        //
        // GET: /Schedule/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Schedule/Create

        [HttpPost]
        public ActionResult Create(Match match)
        {
            if (ModelState.IsValid)
            {
                db.Matchs.Add(match);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(match);
        }
        
        //
        // GET: /Schedule/Edit/5
 
        public ActionResult Edit(int id)
        {
            Match match = db.Matchs.Find(id);
            return View(match);
        }

        //
        // POST: /Schedule/Edit/5

        [HttpPost]
        public ActionResult Edit(Match match)
        {
            if (ModelState.IsValid)
            {
                db.Entry(match).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(match);
        }

        //
        // GET: /Schedule/Delete/5
 
        public ActionResult Delete(int id)
        {
            Match match = db.Matchs.Find(id);
            return View(match);
        }

        //
        // POST: /Schedule/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Match match = db.Matchs.Find(id);
            db.Matchs.Remove(match);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}